package cn.wl.pchart.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

// API测试控制类

@CrossOrigin
@RestController
@RequestMapping("/api")
public class ApiController {
    @RequestMapping(value = "/getSession", method = RequestMethod.POST)
    public JSONObject getSession(HttpServletRequest request, @RequestBody String data) {
        HttpSession session = request.getSession();
        JSONObject json = JSONObject.parseObject(data);
        String key = json.getString("key");
        JSONObject result = new JSONObject();
        if (session.getAttribute(key) != null) {
            result.put("data", session.getAttribute(key).toString());
        } else {
            result.put("data", "没有在session中找到：\"" + key + "\"");
        }
        return result;
    }

//    @RequestMapping(value = "/sessionTest", method = RequestMethod.GET)
//    public JSONObject sessionTest(HttpServletRequest request, @RequestBody String data) {
//        HttpSession session = request.getSession();
//        JSONObject json = JSONObject.parseObject(data);
//        String key = json.getString("key");
//        JSONObject result = new JSONObject();
//        if (session.getAttribute(key) != null) {
//            result.put("data", session.getAttribute(key).toString());
//        } else {
//            session.setAttribute(key, "233");
//            result.put("data", "没有在session中找到：\"" + key + "\"");
//        }
//        return result;
//    }
//
//    @RequestMapping(value = "/sessionTest", method = RequestMethod.GET)
//    public JSONObject sessionTest(HttpServletRequest request, String key) {
//        HttpSession session = request.getSession();
//        System.out.println(session.getId());
//        JSONObject result = new JSONObject();
//        if (session.getAttribute(key) != null) {
//            result.put("data", session.getAttribute(key).toString());
//        } else {
//            session.setAttribute(key, "233");
//            result.put("data", "没有在session中找到：\"" + key + "\"");
//        }
//        return result;
//    }

}
